package com.share.tool.devops.controller;

import com.alibaba.fastjson.JSONObject;
import com.share.common.common.Result;
import com.share.common.enums.RedisChannelEnum;
import com.share.config.redis.RedisClient;
import com.share.tool.devops.model.redis.RedisDelReq;
import com.share.tool.devops.model.redis.RedisListReq;
import com.share.tool.devops.model.redis.RedisVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.PostConstruct;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;

@Slf4j
@Controller
@RequestMapping("/redis")
public class RedisController {
    @Autowired
    private RedisClient redisClient;

    @Autowired
    private RedisClient redisTemplateUtil;

    /**
     * 触发redis生产消息
     *
     * @return
     */
    @PostConstruct
    @PostMapping(value = "/bootAutoConfig")
    public Result reloadBootAutoConfig() {
        int i = 0;
        while (i++ < 1) {
            log.info("触发redis生产消息");
            redisTemplateUtil.convertAndSend(RedisChannelEnum.USER.code, "你好吗");
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        return Result.success("触发redis生产消息");
    }

    @RequestMapping(value = "/toHtml")
    public String test() {
        return "redis/redis"; // html或者其他静态页面的名称
    }

    @ResponseBody
    @RequestMapping("/list")
    public Result<JSONObject> list(RedisListReq req) {
        String regKey = req.getKey();
        if (StringUtils.isEmpty(regKey)) {
            return Result.failure("请求参数不能为空");
        }


        Integer keyCount = 0;
        List<RedisVo> list = new LinkedList<>();
        Set<String> keys = redisClient.keys(regKey);
        if (!CollectionUtils.isEmpty(keys)) {
            keyCount = keys.size();
            int pageSize = req.getPageSize();
            int index = 0;
            Iterator<String> iterator = keys.iterator();
            while (iterator.hasNext() && index < pageSize) {
                String realKey = iterator.next();
                String value = redisClient.get(realKey);
                list.add(new RedisVo(realKey, value));
                index++;
            }
        }

        JSONObject jsonObj = new JSONObject();
        jsonObj.put("list", list);
        jsonObj.put("pageSize", req.getPageSize());
        jsonObj.put("keyCount", keyCount);

        log.info("响应的结果obj={}", jsonObj.toJSONString());
        return Result.success(jsonObj);
    }

    @ResponseBody
    @RequestMapping("/delByKeys")
    public Result<JSONObject> delByKeys(RedisDelReq req) {
        Integer delCount = 0;
        Integer inputCount = 0;

        List<String> keys = req.getKeys();
        if (!CollectionUtils.isEmpty(keys)) {
            inputCount = keys.size();
            for (String key : keys) {
                boolean del = redisClient.del(key);
                if (del) {
                    delCount++;
                }
            }
        }

        JSONObject jsonObj = new JSONObject();
        jsonObj.put("delCount", delCount);
        jsonObj.put("inputCount", inputCount);

        log.info("响应的结果obj={}", jsonObj.toJSONString());
        return Result.success(jsonObj);
    }

    @ResponseBody
    @RequestMapping("/delByReg")
    public Result<JSONObject> delRedisKey(RedisDelReq req) {
        String regKey = req.getRegKey();
        Integer keyCount = 0;
        Integer delCount = 0;
        if (!StringUtils.isEmpty(regKey)) {
            Set<String> keys = redisClient.keys(regKey);
            if (!CollectionUtils.isEmpty(keys)) {
                keyCount = keys.size();
                for (String key : keys) {
                    boolean del = redisClient.del(key);
                    if (del) {
                        delCount++;
                    }
                }
            }
        }

        JSONObject jsonObj = new JSONObject();
        jsonObj.put("regKey", regKey);
        jsonObj.put("delCount", delCount);
        jsonObj.put("keyCount", keyCount);

        log.info("响应的结果obj={}", jsonObj.toJSONString());
        return Result.success(jsonObj);
    }
}
